package com.lenovo.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.lenovo.interceptor.tools.CheckToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author CJF
 * @date 2018/12
 * <p>
 * 拦截器
 */
@Component
public class GeneralInterceptor2 implements HandlerInterceptor {
    @Autowired
    private CheckToken checkToken;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {


        if (checkToken.checkLoginStatus(request)) {
            return true;
        } else {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", 400);
            jsonObject.put("message", "Logon status is invalid, please login again!");
            jsonObject.writeJSONString(response.getWriter());
            return false;
        }

    }


}
